package io.gitee.hfl.rocketmq.handler;

import org.apache.rocketmq.client.apis.producer.SendReceipt;

import java.util.Objects;
import java.util.function.BiConsumer;

/**
 * 生产者发送回调
 *
 * @author hefulin
 * @date 2022/9/7 14:41
 */
public interface ISendCallbackHandler extends BiConsumer<SendReceipt, Throwable> {

    /**
     * 发送回调
     *
     * @param sendReceipt 来自服务器的回执，仅在消息发送成功时才有意义
     * @param throwable   抛出的异常
     */
    @Override
    default void accept(SendReceipt sendReceipt, Throwable throwable) {
        if (Objects.isNull(throwable)) {
            this.onSuccess(sendReceipt);
        } else {
            this.onError(sendReceipt, throwable);
        }
    }

    /**
     * 发送成功回调
     *
     * @param sendReceipt 来自服务器的回执，仅在消息发送成功时才有意义
     */
    void onSuccess(SendReceipt sendReceipt);

    /**
     * 发送失败回调
     *
     * @param sendReceipt 来自服务器的回执，仅在消息发送成功时才有意义
     * @param throwable   发送失败的异常
     */
    void onError(SendReceipt sendReceipt, Throwable throwable);

}
